package com.un.ebs.core.security;

import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

/**
 * @email 496382223@qq.com
 * @author:Lijj
 * @date: 2019/7/25
 */
public class RewriteAccessDenyFilter extends GenericFilterBean {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException {

        try {
            chain.doFilter(request, response);
        } catch (AccessDeniedException e) {
            WriteResult(response,403,"拒绝访问");
        }
    }
    private void WriteResult(ServletResponse response, int code, String msg) throws IOException{
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        WebResult result = WebUtils.error(code,msg);
        response.getWriter().print(WebUtils.getString(result));
    }
}
